///by ability level




clear all
set more off, perm
capture log close
set scheme s2color


cd "C:/Users/msjin/OneDrive/Desktop/interaction/art_passrate"
use "C:/Users/msjin/OneDrive/Desktop/interaction/data_task_panel_cluster_Feb24.dta", clear



capture drop temp
forval i=1/20{
	gen artc2_`i'_h=.
}

forval i=1/13{
	forval j=1/11{
		sum task if levelc2==`i' & repc2==`j' & abi_ct==1
		if (r(N)>0){
		   gen temp=task if levelc2==`i' & repc2==`j' & abi_ct==1
		   bys KIDID levelc2: egen temp2=max(temp)
		   replace artc2_`j'_h= temp2 if indc2==1 & levelc2==`i'
		   drop temp temp2
		}
	}
}


forval i=1/20{
	gen artc2_`i'_m=.
}

forval i=1/13{
	forval j=1/11{
		sum task if levelc2==`i' & repc2==`j' & abi_ct==2
		if (r(N)>0){
		   gen temp=task if levelc2==`i' & repc2==`j' & abi_ct==2
		   bys KIDID levelc2: egen temp2=max(temp)
		   replace artc2_`j'_m= temp2 if indc2==1 & levelc2==`i'
		   drop temp temp2
		}
	}
}

forval i=1/20{
	gen artc2_`i'_l=.
}

forval i=1/13{
	forval j=1/11{
		sum task if levelc2==`i' & repc2==`j' & abi_ct==3
		if (r(N)>0){
		   gen temp=task if levelc2==`i' & repc2==`j' & abi_ct==3
		   bys KIDID levelc2: egen temp2=max(temp)
		   replace artc2_`j'_l= temp2 if indc2==1 & levelc2==`i'
		   drop temp temp2
		}
	}
}



forval i=1/6{
	gen artf2_`i'_h=.
}

forval i=1/7{
	forval j=1/6{
		sum task if levelf2==`i' & repf2==`j' & abi_ft==1
		if (r(N)>0){
		   gen temp=task if levelf2==`i' & repf2==`j' & abi_ft==1
		   bys KIDID levelf2: egen temp2=max(temp)
		   replace artf2_`j'_h= temp2 if indf2==1 & levelf2==`i'
		   drop temp temp2
		}
	}
}


forval i=1/6{
	gen artf2_`i'_m=.
}

forval i=1/7{
	forval j=1/6{
		sum task if levelf2==`i' & repf2==`j' & abi_ft==2
		if (r(N)>0){
		   gen temp=task if levelf2==`i' & repf2==`j' & abi_ft==2
		   bys KIDID levelf2: egen temp2=max(temp)
		   replace artf2_`j'_m= temp2 if indf2==1 & levelf2==`i'
		   drop temp temp2
		}
	}
}


forval i=1/6{
	gen artf2_`i'_l=.
}

forval i=1/7{
	forval j=1/6{
		sum task if levelf2==`i' & repf2==`j' & abi_ft==3
		if (r(N)>0){
		   gen temp=task if levelf2==`i' & repf2==`j' & abi_ft==3
		   bys KIDID levelf2: egen temp2=max(temp)
		   replace artf2_`j'_l= temp2 if indf2==1 & levelf2==`i'
		   drop temp temp2
		}
	}
}


forval i=1/3{
	gen artg_`i'_h=.
}

forval i=1/9{
	forval j=1/3{
		sum task if levelg==`i' & repg==`j' & abi_gt==1
		if (r(N)>0){
		   gen temp=task if levelg==`i' & repg==`j' & abi_gt==1
		   bys KIDID levelg: egen temp2=max(temp)
		   replace artg_`j'_h= temp2 if indg==1 & levelg==`i'
		   drop temp temp2
		}
	}
}


forval i=1/3{
	gen artg_`i'_m=.
}

forval i=1/9{
	forval j=1/3{
		sum task if levelg==`i' & repg==`j' & abi_gt==2
		if (r(N)>0){
		   gen temp=task if levelg==`i' & repg==`j' & abi_gt==2
		   bys KIDID levelg: egen temp2=max(temp)
		   replace artg_`j'_m= temp2 if indg==1 & levelg==`i'
		   drop temp temp2
		}
	}
}

forval i=1/3{
	gen artg_`i'_l=.
}

forval i=1/9{
	forval j=1/3{
		sum task if levelg==`i' & repg==`j' & abi_gt==3
		if (r(N)>0){
		   gen temp=task if levelg==`i' & repg==`j' & abi_gt==3
		   bys KIDID levelg: egen temp2=max(temp)
		   replace artg_`j'_l= temp2 if indg==1 & levelg==`i'
		   drop temp temp2
		}
	}
}

forval i=1/20{
	gen artl_`i'_h=.
}

forval i=1/11{
	forval j=1/20{
		sum task if levell==`i' & repl==`j' & abi_lt==1
		if (r(N)>0){
		   gen temp=task if levell==`i' & repl==`j' & abi_lt==1
		   bys KIDID levell: egen temp2=max(temp)
		   replace artl_`j'_h= temp2 if indl==1 & levell==`i'
		   drop temp temp2
		}
	}
}

forval i=1/20{
	gen artl_`i'_m=.
}

forval i=1/11{
	forval j=1/20{
		sum task if levell==`i' & repl==`j' & abi_lt==2
		if (r(N)>0){
		   gen temp=task if levell==`i' & repl==`j' & abi_lt==2
		   bys KIDID levell: egen temp2=max(temp)
		   replace artl_`j'_m= temp2 if indl==1 & levell==`i'
		   drop temp temp2
		}
	}
}

forval i=1/20{
	gen artl_`i'_l=.
}

forval i=1/11{
	forval j=1/20{
		sum task if levell==`i' & repl==`j' & abi_lt==3
		if (r(N)>0){
		   gen temp=task if levell==`i' & repl==`j' & abi_lt==3
		   bys KIDID levell: egen temp2=max(temp)
		   replace artl_`j'_l= temp2 if indl==1 & levell==`i'
		   drop temp temp2
		}
	}
}

 foreach i of var artl_1_h-artl_20_h artl_1_m-artl_20_m artl_1_l-artl_20_l{
 	gen `i'_N = `i'
 	gen `i'_se = `i'
 }
 order artl_*_N, before(artl_1_h_se)
 preserve
 collapse (count) artl_1_h_N-artl_20_h_N artl_1_m_N-artl_20_m_N artl_1_l_N-artl_20_l_N (mean) artl_1_h-artl_20_h artl_1_m-artl_20_m  artl_1_l-artl_20_l (sem) artl_1_h_se-artl_20_h_se artl_1_m_se-artl_20_m_se artl_1_l_se-artl_20_l_se if indl == 1, by(levell)
 save artl_passrate_byability.dta, replace
 restore

foreach i of var artc2_1_h-artc2_20_h artc2_1_m-artc2_20_m artc2_1_l-artc2_20_l {
	gen `i'_N = `i'
	gen `i'_se = `i'
}
order artc2_*_*_N, before(artc2_1_h_se)
preserve
collapse (count) artc2_1_h_N-artc2_20_h_N artc2_1_m_N-artc2_20_m_N artc2_1_l_N-artc2_20_l_N (mean) artc2_1_h-artc2_20_h artc2_1_m-artc2_20_m artc2_1_l-artc2_20_l  (sem) artc2_1_h_se-artc2_20_h_se artc2_1_m_se-artc2_20_m_se artc2_1_l_se-artc2_20_l_se if indc2 == 1, by(levelc2)
save artc2_passrate_byability.dta, replace
restore

foreach i of var artf2_1_h-artf2_6_h artf2_1_m-artf2_6_m artf2_1_l-artf2_6_l{
	gen `i'_N = `i'
	gen `i'_se = `i'
}
order artf2_*_*_N, before(artf2_1_h_se)
preserve
collapse (count) artf2_1_h_N-artf2_6_h_N artf2_1_m_N-artf2_6_m_N artf2_1_l_N-artf2_6_l_N (mean) artf2_1_h-artf2_6_h artf2_1_m-artf2_6_m artf2_1_l-artf2_6_l (sem) artf2_1_h_se-artf2_6_h_se artf2_1_m_se-artf2_6_m_se artf2_1_l_se-artf2_6_l_se if indf2 == 1, by(levelf2)
save artf2_passrate_byability.dta, replace
restore

foreach i of var artg_1_h-artg_3_h artg_1_m-artg_3_m artg_1_l-artg_3_l{
	gen `i'_N = `i'
	gen `i'_se = `i'
}
order artg_*_*_N, before(artg_1_h_se)
preserve
collapse (count) artg_1_h_N-artg_3_h_N artg_1_m_N-artg_3_m_N artg_1_l_N-artg_3_l_N (mean) artg_1_h-artg_3_h artg_1_m-artg_3_m artg_1_l-artg_3_l (sem) artg_1_h_se-artg_3_h_se artg_1_m_se-artg_3_m_se artg_1_l_se-artg_3_l_se  if indg == 1, by(levelg)
save artg_passrate_byability.dta, replace
restore

****Language****
clear
use artl_passrate_byability.dta, clear

drop if levell == 1



foreach i of num 1/20{
	rename artl_`i'_h artl_h`i'
	rename artl_`i'_h_N artl_hN`i'
	rename artl_`i'_h_se artl_hse`i'
}

foreach i of num 1/20{
	rename artl_`i'_m artl_m`i'
	rename artl_`i'_m_N artl_mN`i'
	rename artl_`i'_m_se artl_mse`i'
}

foreach i of num 1/20{
	rename artl_`i'_l artl_l`i'
	rename artl_`i'_l_N artl_lN`i'
	rename artl_`i'_l_se artl_lse`i'
}


reshape long artl_hN artl_h artl_hse artl_mN artl_m artl_mse artl_lN artl_l artl_lse, i(levell) j(task_order)

drop if artl_hN == 0
drop if artl_mN < 5
drop if artl_lN < 5

gen index = _n
gen ci_hl = artl_h - 1.96*artl_hse
gen ci_hh = artl_h + 1.96*artl_hse
gen ci_ml = artl_m - 1.96*artl_mse
gen ci_mh = artl_m + 1.96*artl_mse
gen ci_ll = artl_l - 1.96*artl_lse
gen ci_lh = artl_l + 1.96*artl_lse



twoway (line artl_h index, sort lcolor(navy))  ///
(line artl_m index, sort lcolor(maroon)) (line ci_ml index, sort lcolor(gray) lpattern(dash)) (line ci_mh index, sort lcolor(gray) lpattern(dash)) ///
(line artl_l index, sort lcolor(maroon)) , ytitle(Fraction of Completing the Task Independently) ///
ylabel(#3) xtitle(Language Difficulty Level)  legend(order(1 2 5 3) row(1)) xline(13 18 25 31 41 58 73 80 89 96, lcolor(khaki)) ///
xlabel(13 18 25 31 41 58 73 80 89 96) title(Average Language Task Passing Rate by Order and Level)
note("Note: The yellow solid lines indicate the last task at each difficulty level. Within difficulty levels," "tasks are arranged by the order of the children taking them.", size(small) span)
graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) ///
plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) 

****Cognitive****
use artc2_passrate_byability.dta, clear




foreach i of num 1/20{
	rename artc2_`i'_h artc2_h`i'
	rename artc2_`i'_h_N artc2_hN`i'
	rename artc2_`i'_h_se artc2_hse`i'
}

foreach i of num 1/20{
	rename artc2_`i'_m artc2_m`i'
	rename artc2_`i'_m_N artc2_mN`i'
	rename artc2_`i'_m_se artc2_mse`i'
}

foreach i of num 1/20{
	rename artc2_`i'_l artc2_l`i'
	rename artc2_`i'_l_N artc2_lN`i'
	rename artc2_`i'_l_se artc2_lse`i'
}

reshape long artc2_hN artc2_h artc2_hse artc2_mN artc2_m artc2_mse artc2_lN artc2_l artc2_lse, i(levelc2) j(task_order)

drop if artc2_hN < 5
drop if artc2_mN < 5
drop if artc2_lN < 5


gen index = _n
gen ci_hl  = artc2_h - 1.96*artc2_hse
gen ci_hh  = artc2_h + 1.96*artc2_hse
gen ci_ml = artc2_m - 1.96*artc2_mse
gen ci_mh = artc2_m + 1.96*artc2_mse
gen ci_ll = artc2_l - 1.96*artc2_lse
gen ci_lh = artc2_l + 1.96*artc2_lse

drop if index == _N
label var ci_ml "95% CI"
label var artc2_m "Mean"
twoway (line artc2_h index, sort lcolor(navy)) (line artc2_m index, sort lcolor(maroon)) ///
(line ci_ml index, sort lcolor(gray) lpattern(dash)) (line ci_mh index, sort lcolor(gray) lpattern(dash)) ///
(line artc2_l index, sort lcolor(maroon)), ytitle(Fraction of Completing the Task Independently) xtitle(Cognitive Difficulty Level) xlabel(,labsize(small)) title(Average Cognitive Task Passing Rate by Task Taking Order, size(med)) ///
 legend(order(1 2 5 3) row(1))  xline(6 14 16 18 28 35 46 53 55 59 65 68, lcolor(khaki)) ///
xlabel(6 14 16 18 28 35 46 53 55 59 65 68) ///
note("Note: The yellow solid lines indicate the last task at each difficulty level. Within difficulty levels," "tasks are arranged by the order of the children taking them.", size(small) span)
graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) ///
plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
saving("cog_passrate_byability", replace)
graph export "cog_passrate_byability.pdf", replace



// save artc2_passrate, replace

****Fine Motor****
use artf2_passrate_byability.dta, clear




foreach i of num 1/6{
	rename artf2_`i'_h artf2_h`i'
	rename artf2_`i'_h_N artf2_hN`i'
	rename artf2_`i'_h_se artf2_hse`i'
}

foreach i of num 1/6{
	rename artf2_`i'_m artf2_m`i'
	rename artf2_`i'_m_N artf2_mN`i'
	rename artf2_`i'_m_se artf2_mse`i'
}

foreach i of num 1/6{
	rename artf2_`i'_l artf2_l`i'
	rename artf2_`i'_l_N artf2_lN`i'
	rename artf2_`i'_l_se artf2_lse`i'
}


reshape long artf2_hN artf2_h artf2_hse artf2_mN artf2_m artf2_mse artf2_lN artf2_l artf2_lse, i(levelf2) j(task_order)

drop if artf2_hN < 5
drop if artf2_mN < 5
drop if artf2_lN < 5

gen index = _n
gen ci_hl = artf2_h - 1.96*artf2_hse
gen ci_hh = artf2_h + 1.96*artf2_hse

gen ci_ml = artf2_m - 1.96*artf2_mse
gen ci_mh = artf2_m + 1.96*artf2_mse

gen ci_ll = artf2_l - 1.96*artf2_lse
gen ci_lh = artf2_l + 1.96*artf2_lse

label var artf2_m "Normal"
label var ci_ml "95% CI"

twoway (line artf2_h index, sort lcolor(navy))  (line artf2_m index, sort lcolor(maroon)) (line ci_ml index, sort lcolor(gray) lpattern(dash)) ///
(line ci_mh index, sort lcolor(gray) lpattern(dash))  (line artf2_l index, sort lcolor(maroon)), ytitle(Fraction of Completing the Task Independently, size(small)) ///
ylabel(0(0.5)1, labsize(small)) xtitle(Fine Motor Difficulty Level, size(small)) xline(6 8 14 20 23 27 30, lcolor(khaki) lwidth(medthin)) ///
xlabel(6 8 14 20 23 27 30, labsize(small)) title(Average Fine Motor Task Passing Rate by Order and Level, size(med))  ///
legend(order(1 2 5 3) row(1) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
note("Note: The yellow solid lines indicate the last task at each difficulty level. Within difficulty levels," "tasks are arranged by the order of the children taking them.", size(small) span) ///
graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) xmtick(, labsize(small)) ymtick(, labsize(small)) ///
plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
saving("fine_passrate_byability", replace)
graph export "fine_passrate_byability.pdf", replace



****Gross Motor****

use artg_passrate_byability.dta, clear




foreach i of num 1/3{
	rename artg_`i'_h artg_h`i'
	rename artg_`i'_h_N artg_hN`i'
	rename artg_`i'_h_se artg_hse`i'
}

foreach i of num 1/3{
	rename artg_`i'_m artg_m`i'
	rename artg_`i'_m_N artg_mN`i'
	rename artg_`i'_m_se artg_mse`i'
}

foreach i of num 1/3{
	rename artg_`i'_l artg_l`i'
	rename artg_`i'_l_N artg_lN`i'
	rename artg_`i'_l_se artg_lse`i'
}

reshape long artg_hN artg_h artg_hse artg_mN artg_m artg_mse artg_lN artg_l artg_lse, i(level) j(task_order)

drop if artg_hN < 5
drop if artg_mN < 5
drop if artg_lN < 5

gen index = _n
gen ci_hl = artg_h - 1.96*artg_hse
gen ci_hh = artg_h + 1.96*artg_hse
gen ci_ml = artg_m - 1.96*artg_mse
gen ci_mh = artg_m + 1.96*artg_mse
gen ci_ll = artg_l - 1.96*artg_lse
gen ci_lh = artg_l + 1.96*artg_lse

twoway (line artg_h index, sort lcolor(navy))  (line artg_m index, sort lcolor(navy)) (line ci_ml index, sort lcolor(gray) lpattern(dash)) ///
(line ci_mh index, sort lcolor(gray) lpattern(dash)) (line artg_l index, sort lcolor(gray) lpattern(dash)), ytitle(Fraction of Completing the Task Independently) ///
ylabel(0(0.2)1, labsize(small)) xtitle(Gross Motor Difficulty Level) xline(1 2 5 7 9 12 15 17, lcolor(khaki)) ///
xlabel(1 2 5 7 9 12 15 17) title(Average Gross Motor Task Passing Rate by Order and Level) ///
legend(order(1 2 5 3) row(1) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
note("Note: The yellow solid lines indicate the last task at each difficulty level. Within difficulty levels," "tasks are arranged by the order of the children taking them.", size(small) span) ///
graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) xmtick(, labsize(small)) ymtick(, labsize(small)) ///
plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
saving("gross_passrate_byability", replace)
graph export "gross_passrate_byability.pdf", replace



